using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace SearchExtension.WebParts
{
    public class SearchExtensionEditorPart : EditorPart
    {
        private TextBox _txtConfigXml;
        private CheckBox _chkDisable;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (_txtConfigXml == null)
            {
                HtmlGenericControl lbl = new HtmlGenericControl("span");
                lbl.InnerText = "Configuration XML";
                lbl.Style.Add("display", "block");
                Controls.Add(lbl);

                _txtConfigXml = new TextBox();
                _txtConfigXml.Style.Add("width", "100%");
                _txtConfigXml.TextMode = TextBoxMode.MultiLine;
                _txtConfigXml.Attributes.Add("class", "UserInput");
                _txtConfigXml.Rows = 10;

                Controls.Add(_txtConfigXml);
            }
            if (_chkDisable == null)
            {
                HtmlGenericControl lbl = new HtmlGenericControl("span");
                lbl.InnerText = " ";
                lbl.Style.Add("display", "block");
                lbl.Style.Add("padding-top", "10px");
                Controls.Add(lbl);

                _chkDisable = new CheckBox();
                _chkDisable.Text = "Disable search extension";
                Controls.Add(_chkDisable);
            }
        }

        public override void SyncChanges()
        {
            // Set text field values to equal the properties from the webpart

            this.EnsureChildControls();

            SearchExtensionWebPart searchExtensionWebPart = (SearchExtensionWebPart)this.WebPartToEdit;
            _txtConfigXml.Text = searchExtensionWebPart.ConfigXml;
            _chkDisable.Checked = searchExtensionWebPart.ExtensionDisabled;
        }

        public override bool ApplyChanges()
        {
            // Apply settings back to the web part

            this.EnsureChildControls();

            SearchExtensionWebPart searchExtensionWebPart = (SearchExtensionWebPart)this.WebPartToEdit;
            searchExtensionWebPart.ConfigXml = _txtConfigXml.Text;
            searchExtensionWebPart.ExtensionDisabled = _chkDisable.Checked;

            return true;
        }
    }
}
